package com.bocloud.cmp.driver.zteadapter;

import java.util.List;
import java.util.Map;

import com.bocloud.cmp.driver.utils.Configure;
import com.bocloud.cmp.driver.utils.DateTimeTool;
import com.bocloud.cmp.model.MonitorCategoryModel;
import com.bocloud.cmp.model.MonitorModel;
import com.bocloud.common.enums.PostDataFormat;
import com.bocloud.common.model.BsmResult;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

public class ZTEServerPerf {

    private final String zteurl = "zx.openstack.url";
    private final String zteuuid = "zx.openstack.uuid";

    Map<String, Object> params;

    public ZTEServerPerf() {
        super();
    }

    public ZTEServerPerf(Map<String, Object> params) {
        super();
        this.params = params;
    }

    /**
     * 性能接口调用
     * 
     * @param type
     * @param name
     * @param time
     * @return
     */
    public BsmResult serverperf(String type, String name, String time) {
        Configure config = new Configure("config/application.properties");
        String zteur = config.getkey(zteurl);
        String zteuui = config.getkey(zteuuid);
        Map<String, Object> params = Maps.newHashMap();
        params.put("dcId", zteuui);
        params.put("period", converttime(time));
        params.put("type", converttype(type));
        params.put("startTime", timeOffset(time));// problems
        params.put("endTime", DateTimeTool.getUTCtime(0));

        List<ServerStaticsModel> result = new ZTEHttpRequest(zteur, zteuui, PostDataFormat.RAW)
                .getInstanceStatics(params);
        if (null != result) {
            return new BsmResult(true, convertMoniter(result, name, type), "获取性能数据成功！");
        }
        return new BsmResult(false, "获取性能数据失败！");
    }

    /**
     * 时间段转换
     * 
     * @param time
     * @return
     */
    private String timeOffset(String time) {
        if ("day".equalsIgnoreCase(time)) {
            return DateTimeTool.getUTCtime(-24);
        } else if ("week".equalsIgnoreCase(time)) {
            return DateTimeTool.getUTCtime(-24 * 7);
        } else if ("month".equalsIgnoreCase(time)) {
            return DateTimeTool.getUTCtime(-24 * 30);
        } else if ("real".equalsIgnoreCase(time)) {
            return DateTimeTool.getUTCtime(-2);
        }
        return DateTimeTool.getUTCtime(-2);
    }

    /**
     * 转换时间 ZTE
     * 
     * @param time
     * @return
     */
    private String converttime(String time) {
        if ("day".equalsIgnoreCase(time)) {
            return "1";// 小时
        } else if ("week".equalsIgnoreCase(time)) {
            return "2";// 天
        } else if ("month".equalsIgnoreCase(time)) {
            return "2";// 天
        } else if ("real".equalsIgnoreCase(time)) {
            return "4";// 5分钟
        }
        return "4";
    }

    /**
     * 类型转换
     * 
     * @param type
     * @return
     */
    private String converttype(String type) {
        if ("CPU".equalsIgnoreCase(type)) {
            return "cpuUtil";
        } else if ("MEM".equalsIgnoreCase(type)) {
            return "memUtil";
        } else if ("DISK".equalsIgnoreCase(type)) {
            return "diskUtil";
        }
        return "CPU";
    }

    /**
     * 格式转换
     * 
     * @param result
     * @param name
     * @param type
     * @return
     */
    private MonitorModel convertMoniter(List<ServerStaticsModel> result, String name, String type) {
        if (null == result) {
            return null;
        }
        MonitorModel monitroModel = new MonitorModel();
        List<String> keys = Lists.newArrayList();
        List<MonitorCategoryModel> values = Lists.newArrayList();
        MonitorCategoryModel monitorCate = new MonitorCategoryModel();
        monitorCate.setName(type);
        List<String> datas = Lists.newArrayList();
        for (ServerStaticsModel serverStatic : result) {
            if (name.equalsIgnoreCase(serverStatic.getName())) {
                keys.add(serverStatic.getDatetime());
                datas.add(serverStatic.getValue());
            }
        }
        monitorCate.setData(datas);
        values.add(monitorCate);
        monitroModel.setKeys(keys);
        monitroModel.setValues(values);
        return monitroModel;
    }
}
